package com.loongtech.bi.support;

import com.loongtech.bi.action.DataBiAction;
import com.loongtech.bi.action.newsystem.SyncSysConfigAction;
import com.loongtech.bi.action.newsystem.SystemConfig;
import com.loongtech.bi.manager.system.*;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class Sys implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private BiPlatformManager biPlatformManager;
    @Autowired
    private GameParamTypeConfigManager gameParamTypeManager;
    @Autowired
    private GameParamConfigManager gameParamManager;
    @Autowired
    private ConfigManager configManager;
    @Autowired
    private BiMonitorConfigManager biMonitorConfigManager;
    @Autowired
    private DailyReportManager dailyReportManager;
    @Resource
    private SysProjectManager sysProjectManager;
    @Autowired
    private SystemConfig systemConfig;
    @Resource
    private SyncSysConfigAction syncSysConfigAction;

    private boolean isInit = false;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent arg0) {
        if (isInit) {
            return;
        }
        isInit = true;
        init();
    }

    public void init() {
        Log.bi.info("REPORT-SDK START------INIT CONFIG!");

       /* String serverInfo = null;
        try {
            serverInfo = Utils.httpGet("http://10.236.44.188:8080/overviewBiAction/search.do?date=2017-06-14&cdate=2017-06-15&platformid=0&channelid=0&serverid=0", null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("-------------------------------------------------------------------------------------------------");
        System.out.println(serverInfo);
        System.out.println("-------------------------------------------------------------------------------------------------");
*/
        biPlatformManager.init();

        sysProjectManager.init();
        gameParamTypeManager.init();
        gameParamManager.init();

        configManager.init();

        // 向biroot发送得启信息
        /*try {
            String url = configManager.getConfigValueByName(ConfigManager.PROJECT_URL);
            if (StringUtils.isNotEmpty(url)) {
                AsyncHttpClientUtils.cidRestart(url + "/resources/pages/rest.jsf?method=cidRestart&project=" + configManager.getConfigValueByName(ConfigManager.PROJECT_NAME));
            } else {
                AsyncHttpClientUtils.cidRestart("");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }*/

        if ("mid".equals(systemConfig.getServerType())) {
            new Thread(new Runnable() {
                public void run() {
                    try {
                        while (true) {
                            DataBiAction.RESULT_DATA_ALIVE_30SEC.clear();
                            Thread.sleep(Time.SEC * 30);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            new Thread(new Runnable() {
                public void run() {
                    try {
                        while (true) {
                            DataBiAction.RESULT_DATA_ALIVE_30MIN.clear();
                            Thread.sleep(Time.MIN * 30);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            new Thread(new Runnable() {
                public void run() {
                    try {
                        while (true) {
                            DataBiAction.RESULT_DATA_ALIVE_3HOUR.clear();
                            Thread.sleep(Time.HOUR * 3);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            //系统参数同步
            new Thread(new Runnable() {
                public void run() {
                    try {
                        while (true) {
                            syncSysConfigAction.callsyncToDatabase();
                            Thread.sleep(Time.MIN * 2);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();

        } else {
            // 系统启动时，加载同步服务器列表的job
            biMonitorConfigManager.addSyncServerMonitorConfig();

            String stop = configManager.getConfigValueByName(ConfigManager.STOP_MONITOR);
            if (stop == null || !"1".equals(stop)) {
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            Thread.sleep(Time.MIN * 10);

                            // 系统启动时，加载全部监控job
                            biMonitorConfigManager.addAllEntityMonitorConfig();
                            // 系统启动时，加载报表任务
                            dailyReportManager.addAllEntityDaliyReport();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }


       /* File file = new File("c://bi_advconfig.xlsx");
        try {
            parseUploadedExcel(file);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
    }


   /* @Resource
    public AdvCostManager managerBase;
    public void parseUploadedExcel(File file) throws Exception {
        InputStream input = new FileInputStream(file);
        Workbook wb = WorkbookFactory.create(input);
        Sheet tempSheet = wb.getSheetAt(0);
        if (tempSheet == null) {
            IOUtils.closeQuietly(input);
            System.out.println("Error---------------------------excel内容为空,数据错误！");
            throw new Exception("excel内容为空,数据错误");
        }
        int rowNum = tempSheet.getLastRowNum();
        for (int i = 0; i <= rowNum; i++) {
            Row row = tempSheet.getRow(i);
            if (row == null) {
                continue;
            }
            String ldate = ExcelUtils.getCellValue(row.getCell(0));
            String platform = ExcelUtils.getCellValue(row.getCell(1));
            String agent = ExcelUtils.getCellValue(row.getCell(2));
            String channelName = ExcelUtils.getCellValue(row.getCell(3));
            String discount = ExcelUtils.getCellValue(row.getCell(6));
            String roi = ExcelUtils.getCellValue(row.getCell(7));

            EntityAdvCost e = new EntityAdvCost();
            e.setId(i);
            e.setLdate(ldate);
            e.setPlatform(platform);
            e.setAgent(agent);
            e.setChannelName(channelName);
            e.setDiscount(discount);
            e.setCost(discount);
            e.setRebate("0");
            e.setRoi(roi);

            managerBase.persist(e);
        }

        IOUtils.closeQuietly(input);
    }*/
}
